package com.mystyle.le.weekproject.activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.mystyle.le.weekproject.R;
import com.mystyle.le.weekproject.adapter.CuttorCostorAdapteri;
import com.mystyle.le.weekproject.beans.SqliteData;
import com.mystyle.le.weekproject.sqlite.MySqlite;

import java.util.ArrayList;
import java.util.List;
/*
* 漫画收藏
* */

public class CuttorActivity extends AppCompatActivity {
    private List<SqliteData> list;
    private ListView cuttor_LV;
    private SQLiteDatabase db;
    private static MySqlite mySqlite;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cuttor);
        initData();
    }

    private void initData() {
        cuttor_LV = ((ListView) findViewById(R.id.cuttor_LV));
            list = new ArrayList<>();
            //获取数据库路径
            mySqlite = new MySqlite(CuttorActivity.this);
            db = mySqlite.getReadableDatabase();
            String sql = "select *from data";
            Cursor cursor = db.rawQuery(sql,null);
            CuttorCostorAdapteri cuttorAdapter = new CuttorCostorAdapteri(CuttorActivity.this,cursor);
            cuttor_LV.setAdapter(cuttorAdapter);
            while (cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String ranking = cursor.getString(cursor.getColumnIndex("ranking"));
                String state = cursor.getString(cursor.getColumnIndex("state"));
                String id1 = cursor.getString(cursor.getColumnIndex("id1"));
                String icon = cursor.getString(cursor.getColumnIndex("icon"));
                list.add(new SqliteData(name, id1, ranking, state, icon));
            }
            cuttor_LV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    SqliteData data1 = list.get(position);
                    String id1 = data1.getId1();
                    Intent intent1 = new Intent(CuttorActivity.this,DetialActivity.class);
                    intent1.putExtra("manager_cuttor",id1);
                    startActivity(intent1);
//                    finish();
                }
            });
    }
}
